﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace WP7.Extensions.Extensions
{
    public static class StringExtensions
    {
        /// <summary>
        /// Returns integer from string value
        /// </summary>
        /// <param name="self">Your string</param>
        /// <returns>Returns integer from string value</returns>
        public static int ToInt(this string self)
        {
            int outcome;
            bool result = int.TryParse(self, out outcome);

            if (!result)
                throw new InvalidOperationException("Value is not valid.");

            return outcome;
        }

        /// <summary>
        /// Checks if string is null or empty in much cleaner code
        /// </summary>
        /// <returns>True if string is not null or empty</returns>
        public static bool HasValue(this string self)
        {
            return !String.IsNullOrEmpty(self);
        }

        /// <summary>
        /// Checks if string is null or empty in much cleaner code
        /// </summary>
        /// <returns>True if string is not null or empty</returns>
        public static bool IsNullOrEmpty(this string self)
        {
            return String.IsNullOrEmpty(self);
        }

        /// <summary>
        /// Shorthand for String.Format() - F for format. Results in much cleaner code.
        /// </summary>
        /// <param name="self">Format - same as String.Format's format parameter</param>
        /// <param name="args">Objects to format</param>
        /// <returns>Formated string</returns>
        public static string Format(this string self, params object[] args)
        {
            if (self == null)
                throw new ArgumentNullException("format");

            return string.Format(self, args);
        }

        /// <summary>
        /// Checks if string is a valid email address
        /// </summary>
        /// <param name="self">Your string</param>
        /// <returns>True if string is valid email address</returns>
        public static bool IsValidEmailAddress(this string self)
        {
            var regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
            return regex.IsMatch(self);
        }

        ///<summary>
        /// Returns a string array that contains the substrings in this string that are delimited by a specified character
        ///</summary>
        ///<param name="self">Your string</param>
        ///<param name="separator">Character to use for split string with</param>
        ///<returns>Array of substrings</returns>
        public static IEnumerable<string> Split(this string self, char separator)
        {
            return self.Split(new[] { separator });
        }

        ///<summary>
        /// Returns a string array that contains the substrings in this string that are delimited by a specified string
        ///</summary>
        ///<param name="self">Your string</param>
        ///<param name="separator">String to use for split original string with</param>
        ///<returns>Array of substrings</returns>
        public static IEnumerable<string> Split(this string self, string separator)
        {
            return self.Split(new[] { separator }, StringSplitOptions.None);
        }

      
        /// <summary>
        /// Trims string from suplied character at begining and end
        /// </summary>
        /// <param name="self">Your string</param>
        /// <param name="character">Character to trim with</param>
        /// <returns>Trimed string</returns>
        public static string Trim(this string self, char character)
        {
            return self.Trim(new char[] {character});
        }
    }
}
